package com.pqy.ts.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pqy.ts.entity.TLike;
import com.pqy.ts.entity.vo.R;
import com.pqy.ts.service.TLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author panqiyi
 * @since 2022-06-09
 */
@RestController
@RequestMapping("/ts/tlike")
@CrossOrigin
public class TLikeController {

    @Autowired
    private TLikeService likeService;

    @PostMapping("likeControl")
    public R likeControl(@RequestBody TLike like){

        //检查用户是否登录  : session 中 ，登录的用户才能点赞，否则返回提示信息，暂时不做

        //查询 该用户 对 该条评论 是否点赞过，如果点赞过则取消点赞，没有则增加点赞
        String commentId = like.getCommentId();
        String userId = like.getUserId();

        QueryWrapper<TLike> wrapper = new QueryWrapper<>();
        wrapper.eq("comment_id",commentId);
        wrapper.eq("user_id",userId);
        TLike tLike = likeService.getOne(wrapper);

        if (tLike==null){
            // 没有点赞，则点赞+1
            likeService.save(like);
        }else {
            // 点过赞，点赞-1
            likeService.removeById(tLike.getId());
        }

        return R.ok();
    }

}

